package juc;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

@Data
@NoArgsConstructor
@AllArgsConstructor
class User{
    private Integer id;
    private String userName;
    private int age;
}

/**
 * 函数式接口
 *
 * 流式编程
 */
public class StreamDemo {

    public static void main(String args[]){
        User u1 = new User(11,"a",23);
        User u2 = new User(12,"b",24);
        User u3 = new User(13,"c",22);
        User u4 = new User(14,"d",28);
        User u5 = new User(16,"e",26);

        List<User> list = Arrays.asList(u1,u2,u3,u4,u5);

        list.stream().filter(u -> {return u.getId() % 2 == 0;}).filter(u -> {return u.getAge() > 24;}).map(m -> {return m.getUserName().toUpperCase();}).sorted((s1,s2) -> {return s2.compareTo(s1);}).limit(1).forEach(System.out::println);

    }

    private static void functionInterface() {
        //        Function<String,Integer> function = new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                return 1024;
//            }
//        };
//        System.out.println(function.apply("sss"));

        Function<String,Integer> function = s -> {return s.length();};
        System.out.println(function.apply("abc"));

//        Predicate<String> predicate = new Predicate<String>() {
//            @Override
//            public boolean test(String s) {
//                return false;
//            }
//        };

        Predicate<String> predicate = s -> {return s.isEmpty();};
        System.out.println(predicate.test("sss"));

//        Consumer<String> consumer = new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//
//            }
//        };

        Consumer<String> consumer = s -> {System.out.println(s);};
        consumer.accept("sssssss");


//        Supplier<String> supplier = new Supplier<String>() {
//            @Override
//            public String get() {
//                return null;
//            }
//        };

        Supplier<String> supplier = () -> {return "ss";};
        System.out.println(supplier.get());
    }

}
